package com.py.chat.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.PreparedStatement;
import com.py.chat.bean.User;


public class DBUtils {

	private DBUtils() {
		
	}
	private static Connection conn = getCon();

	private static Connection getCon(){
		 String URL= PropertiesUtil.getValue("url");
	     String USER= PropertiesUtil.getValue("user");
	     String PASSWORD= PropertiesUtil.getValue("password");
	     try {
			//1.加载驱动程序
			 Class.forName("com.mysql.jdbc.Driver");
			 //2.获得数据库链接
			 conn = DriverManager.getConnection(URL, USER, PASSWORD);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     return conn;
	}
	
	public static Connection getConnection(){
		return conn;
	}
	
	public static void main(String[] args) throws Exception{
		Connection conn = DBUtils.getConnection();
		    String sql = "select * from user";
		    PreparedStatement pstmt;
		    try {
		        pstmt = (PreparedStatement)conn.prepareStatement(sql);
		        ResultSet rs = pstmt.executeQuery();
		        int col = rs.getMetaData().getColumnCount();
		        System.out.println("============================");
		        while (rs.next()) {
		            for (int i = 1; i <= col; i++) {
		                System.out.print(rs.getString(i) + "\t");
		                if ((i == 2) && (rs.getString(i).length() < 8)) {
		                    System.out.print("\t");
		                }
		             }
		            System.out.println("");
		        }
		            System.out.println("============================");
		    } catch (SQLException e) {
		        e.printStackTrace();
		    }
	}
	
}
